package note;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class NoteConvertManage {

    private static final String CHAPTER = "◆";
    private static final String ORI_ZHU = "注: ";
    private static final String WX_NOTE_HEAD = ">> ";

    /**
     * 从文件中读取文本到 List<String>
     */
    @SneakyThrows
    public List<String> readTxtFromFile(String filePathName) {
        Path path = Paths.get(filePathName);
        return Files.readAllLines(path);
    }

    /**
     * 将 List<String> 写入本地文本
     */
    @SneakyThrows
    public void writeListToTxt(List<String> txts, String filePathName) {
        Path path = Paths.get(filePathName);

        for (int i = 0; i < txts.size(); i++) {
            if (i == 0) {
                Files.write(path, txts.get(i).getBytes());
            } else {
                try (BufferedWriter writer = Files.newBufferedWriter(path, StandardOpenOption.APPEND)) {
                    writer.write("\n" + txts.get(i));
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 处理列表内文本
     */
    public List<String> convertTxt(List<String> txtsFull) {

        // 移除符合 yyyy-mm-dd 格式的行
        List<String> txts = txtsFull.stream()
                .filter(x -> !isDateStr(x))
                .collect(Collectors.toList());

        for (int i = 0; i < txts.size(); i++) {
            if (i == 0) {
                /* book name */
                log.info("book name is: {}", txts.get(i));
                continue;
            }
            if (i == 1 && StringUtils.isBlank(txts.get(i))) {
                txts.remove(i);
                log.info("remove the second <null> line");
                i--;
                continue;
            } else if (i == 1 && StringUtils.isNotBlank(txts.get(i))) {
                /* book author */
                log.info("book author is: {}", txts.get(i));
                txts.add(2, "");
                i++;
                continue;
            }

            // 空行跳出
            if (StringUtils.isBlank(txts.get(i))) {
                continue;
            }

            // 「空格」开头的为章节
            if (StringUtils.startsWith(txts.get(i), " ")) {
                txts.set(i, CHAPTER + txts.get(i));
                txts.add(i + 1, "");
                i = i + 2;
            }

            if (StringUtils.startsWith(txts.get(i), ORI_ZHU)) {
                // 「注: 」开头的为注释
                String tmpStr = txts.get(i);
                txts.set(i, txts.get(i - 1));
                txts.set(i - 1, StringUtils.replace(tmpStr, ORI_ZHU, ""));
            } else {
                // 其他开头的为划线部分
                txts.set(i, WX_NOTE_HEAD + txts.get(i));
                if (StringUtils.isNotBlank(txts.get(i - 1))) {
                    txts.add(i, "");
                    i++;
                }
            }
        }
        return txts;
    }

    /**
     * 判断字符串是否为 yyyy-mm-dd 格式
     */
    public boolean isDateStr(String input) {
        if (StringUtils.isBlank(input)) {
            return false;
        }
        try {
            String newInput = StringUtils.trim(input);
            DateUtils.parseDate(newInput, "yyyy-MM-dd");
            log.info("the line is date format, {}", input);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

}
